package com.wm.web.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.UserComment;
import com.wm.comm.Constant;

import com.wm.service.UserCommentService;
import com.wm.web.servlet.UserSession;

public class UserCommentAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(UserCommentAction.class);
	
	private UserCommentService userCommentService;

	public UserCommentService getUserCommentService() {
		return userCommentService;
	}

	public void setUserCommentService(UserCommentService userCommentService) {
		this.userCommentService = userCommentService;
	}
	
	private Map session;

	public void setSession(Map session){
    	this.session = session;
    }
	
	public Map getSession() {
		return session;
	}
	
	private String message="";
	private List list;
	private String verifycode;
	private String error;
	private String returnURL;

	public String getReturnURL() {
		return returnURL;
	}

	public void setReturnURL(String returnURL) {
		this.returnURL = returnURL;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)==null){
				this.setReturnURL(Constant.WEB_URL+"userComment.action");
				this.setAction("index");
				return "loginAction";
			}
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		this.setList(this.getUserCommentService().findUserCommentByUserId(us.getUid()));
		if(this.getError()!=null&&!this.getError().equals("")){
			this.addFieldError("error", this.getText(this.getError()));
		}
		return "index";
	}
	
	public String save(){
		//验证码正确
		if(!getSessionAuthCode().equalsIgnoreCase(verifycode)){
			this.setError("error.autherror");
			this.setAction("index");
			return "indexAction";
		}
		if(this.getMessage()==null || this.getMessage().trim().equals("")){
			this.setError("error.message.null");
			this.setAction("index");
			return "indexAction";
		}
		if(this.getMessage().length()>500){
			this.setError("error.message.length");
			this.setAction("index");
			return "indexAction";
		}
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		UserComment uc = new UserComment();
		uc.setMessage(message);
		uc.setAddTime(new Date());
		uc.setUserId(us.getUid());
		this.getUserCommentService().saveUserComment(uc);
		this.setAction("index");
		return "indexAction";
	}

	private String getSessionAuthCode() {
        String auth = (String) this.getSession().get("authCode");
        if(StringUtils.isBlank(auth)){
        	return "";
        }else{
        	return auth;
        }
    }
}
